package com.tensquare.spit.service;

import com.tensquare.spit.dao.SpitDao;
import com.tensquare.spit.pojo.Spit;
import entity.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.List;
import java.util.Optional;

@Service
public class SpitService {

    @Autowired
    private SpitDao spitDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;

    //查询全部吐槽列表
    public List<Spit> findAllSpit() {
        List<Spit> all = spitDao.findAll();
        return all;
    }
    //根据ID查询吐槽
    public Spit findBySpitId(String spitId) {
        return spitDao.findById(spitId).get();
    }
    //增加吐槽
    public void addSpit(Spit spit) {
        //使用自定的自增id
        IdWorker idWorker=new IdWorker();
        spit.set_id(idWorker.nextId()+"");
        spitDao.save(spit);
    }
    //修改吐槽
    public void updateSpitId(String id) {
        //
        Query query=new Query();
        //
        query.addCriteria(Criteria.where("_id").is(id));
        Update update=new Update();
        update.inc("thumbup",1);
        mongoTemplate.updateFirst(query,update,"spit");
    }
    //
    public void updateSpitId2(String id){
        Spit spit = spitDao.findById(id).get();
        spit.setThumbup(spit.getThumbup()+1);
        spitDao.save(spit);
    }
    //根据ID删除吐槽
    public void deleteSpit(String spitId) {
        spitDao.deleteById(spitId);
    }
    //上级ID查询吐槽列表
    public PageResult<Spit> findByParentid(String parentid, int page, int size) {
        Page<Spit> byParentid = spitDao.findByParentid(parentid, PageRequest.of(page - 1, size));
        return new PageResult<>(byParentid.getTotalElements(),byParentid.getContent());
    }
    //吐槽点赞
    public Boolean addThumbup(String spitId) {
        System.out.println("spitId = " + spitId);
        String userid="1013";
        //从缓存中获取数据
        String spit1 = (String) redisTemplate.opsForValue().get("thumbup_" + userid+"_"+spitId);
        System.out.println("spit1 = " + spit1+"___________");
        if(!StringUtils.isEmpty(spit1)){
            System.out.println("222");
            return false;
        }else{
            //调用修改
            System.out.println("111");
            updateSpitId2(spitId);
            Spit spit = spitDao.findById(spitId).get();
            System.out.println("spit = " + spit);
            Integer thumbup = spit.getThumbup();

            System.out.println("thumbup = " + thumbup);
            //将点赞成功后的吐槽信息放到redis中
            redisTemplate.opsForValue().set("thumbup_" + userid+"_"+spitId,thumbup+"");
            return true;
        }

    }
}
